home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 39 / Amiga Format CD39 (1999-04-13)(Future Publishing)(GB)[!][issue 1999-05].iso / -seriously_amiga- / graphics / mpeg2decode / src / idct.c < prev    next >
C/C++ Source or Header  |  1999-03-02  |  6KB  |  224 lines

  1. /* idct.c, inverse fast discrete cosine transform                           */
  2.  
  3. /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
  4.  
  5. /*
  6.  * Disclaimer of Warranty
  7.  *
  8.  * These software programs are available to the user without any license fee or
  9.  * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
  10.  * any and all warranties, whether express, implied, or statuary, including any
  11.  * implied warranties or merchantability or of fitness for a particular
  12.  * purpose.  In no event shall the copyright-holder be liable for any
  13.  * incidental, punitive, or consequential damages of any kind whatsoever
  14.  * arising from the use of these programs.
  15.  *
  16.  * This disclaimer of warranty extends to the user of these programs and user's
  17.  * customers, employees, agents, transferees, successors, and assigns.
  18.  *
  19.  * The MPEG Software Simulation Group does not represent or warrant that the
  20.  * programs furnished hereunder are free of infringement of any third-party
  21.  * patents.
  22.  *
  23.  * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
  24.  * are subject to royalty fees to patent holders.  Many of these patents are
  25.  * general enough such that they are unavoidable regardless of implementation
  26.  * design.
  27.  *
  28.  */
  29.  
  30. /**********************************************************/
  31. /* inverse two dimensional DCT, Chen-Wang algorithm       */
  32. /* (cf. IEEE ASSP-32, pp. 803-816, Aug. 1984)             */
  33. /* 32-bit integer arithmetic (8 bit coefficients)         */
  34. /* 11 mults, 29 adds per DCT                              */
  35. /*                                      sE, 18.8.91       */
  36. /**********************************************************/
  37. /* coefficients extended to 12 bit for IEEE1180-1990      */
  38. /* compliance                           sE,  2.1.94       */
  39. /**********************************************************/
  40.  
  41. /* this code assumes >> to be a two's-complement arithmetic */
  42. /* right shift: (-2)>>1 == -1 , (-3)>>1 == -2               */
  43.  
  44. #include "config.h"
  45.  
  46. #define W1 2841 /* 2048*sqrt(2)*cos(1*pi/16) */
  47. #define W2 2676 /* 2048*sqrt(2)*cos(2*pi/16) */
  48. #define W3 2408 /* 2048*sqrt(2)*cos(3*pi/16) */
  49. #define W5 1609 /* 2048*sqrt(2)*cos(5*pi/16) */
  50. #define W6 1108 /* 2048*sqrt(2)*cos(6*pi/16) */
  51. #define W7 565  /* 2048*sqrt(2)*cos(7*pi/16) */
  52.  
  53. /* global declarations */
  54. void Initialize_Fast_IDCT _ANSI_ARGS_((void));
  55. void Fast_IDCT _ANSI_ARGS_((short *block));
  56.  
  57. /* private data */
  58. static short iclip[1024]; /* clipping table */
  59. static short *iclp;
  60.  
  61. /* private prototypes */
  62. static void idctrow _ANSI_ARGS_((short *blk));
  63. static void idctcol _ANSI_ARGS_((short *blk));
  64.  
  65. /* row (horizontal) IDCT
  66.  *
  67.  *           7                       pi         1
  68.  * dst[k] = sum c[l] * src[l] * cos( -- * ( k + - ) * l )
  69.  *          l=0                      8          2
  70.  *
  71.  * where: c[0]    = 128
  72.  *        c[1..7] = 128*sqrt(2)
  73.  */
  74.  
  75. #ifdef STORM
  76. static void idctrow(short *blk)
  77. #else
  78. static void idctrow(blk)
  79. short *blk;
  80. #endif
  81. {
  82.   int x0, x1, x2, x3, x4, x5, x6, x7, x8;
  83.  
  84.   /* shortcut */
  85.   if (!((x1 = blk[4]<<11) | (x2 = blk[6]) | (x3 = blk[2]) |
  86.         (x4 = blk[1]) | (x5 = blk[7]) | (x6 = blk[5]) | (x7 = blk[3])))
  87.   {
  88.     blk[0]=blk[1]=blk[2]=blk[3]=blk[4]=blk[5]=blk[6]=blk[7]=blk[0]<<3;
  89.     return;
  90.   }
  91.  
  92.   x0 = (blk[0]<<11) + 128; /* for proper rounding in the fourth stage */
  93.  
  94.   /* first stage */
  95.   x8 = W7*(x4+x5);
  96.   x4 = x8 + (W1-W7)*x4;
  97.   x5 = x8 - (W1+W7)*x5;
  98.   x8 = W3*(x6+x7);
  99.   x6 = x8 - (W3-W5)*x6;
  100.   x7 = x8 - (W3+W5)*x7;
  101.   
  102.   /* second stage */
  103.   x8 = x0 + x1;
  104.   x0 -= x1;
  105.   x1 = W6*(x3+x2);
  106.   x2 = x1 - (W2+W6)*x2;
  107.   x3 = x1 + (W2-W6)*x3;
  108.   x1 = x4 + x6;
  109.   x4 -= x6;
  110.   x6 = x5 + x7;
  111.   x5 -= x7;
  112.   
  113.   /* third stage */
  114.   x7 = x8 + x3;
  115.   x8 -= x3;
  116.   x3 = x0 + x2;
  117.   x0 -= x2;
  118.   x2 = (181*(x4+x5)+128)>>8;
  119.   x4 = (181*(x4-x5)+128)>>8;
  120.   
  121.   /* fourth stage */
  122.   blk[0] = (x7+x1)>>8;
  123.   blk[1] = (x3+x2)>>8;
  124.   blk[2] = (x0+x4)>>8;
  125.   blk[3] = (x8+x6)>>8;
  126.   blk[4] = (x8-x6)>>8;
  127.   blk[5] = (x0-x4)>>8;
  128.   blk[6] = (x3-x2)>>8;
  129.   blk[7] = (x7-x1)>>8;
  130. }
  131.  
  132. /* column (vertical) IDCT
  133.  *
  134.  *             7                         pi         1
  135.  * dst[8*k] = sum c[l] * src[8*l] * cos( -- * ( k + - ) * l )
  136.  *            l=0                        8          2
  137.  *
  138.  * where: c[0]    = 1/1024
  139.  *        c[1..7] = (1/1024)*sqrt(2)
  140.  */
  141. #ifdef STORM
  142. static void idctcol(short *blk)
  143. #else
  144. static void idctcol(blk)
  145. short *blk;
  146. #endif
  147. {
  148.   int x0, x1, x2, x3, x4, x5, x6, x7, x8;
  149.  
  150.   /* shortcut */
  151.   if (!((x1 = (blk[8*4]<<8)) | (x2 = blk[8*6]) | (x3 = blk[8*2]) |
  152.         (x4 = blk[8*1]) | (x5 = blk[8*7]) | (x6 = blk[8*5]) | (x7 = blk[8*3])))
  153.   {
  154.     blk[8*0]=blk[8*1]=blk[8*2]=blk[8*3]=blk[8*4]=blk[8*5]=blk[8*6]=blk[8*7]=
  155.       iclp[(blk[8*0]+32)>>6];
  156.     return;
  157.   }
  158.  
  159.   x0 = (blk[8*0]<<8) + 8192;
  160.  
  161.   /* first stage */
  162.   x8 = W7*(x4+x5) + 4;
  163.   x4 = (x8+(W1-W7)*x4)>>3;
  164.   x5 = (x8-(W1+W7)*x5)>>3;
  165.   x8 = W3*(x6+x7) + 4;
  166.   x6 = (x8-(W3-W5)*x6)>>3;
  167.   x7 = (x8-(W3+W5)*x7)>>3;
  168.   
  169.   /* second stage */
  170.   x8 = x0 + x1;
  171.   x0 -= x1;
  172.   x1 = W6*(x3+x2) + 4;
  173.   x2 = (x1-(W2+W6)*x2)>>3;
  174.   x3 = (x1+(W2-W6)*x3)>>3;
  175.   x1 = x4 + x6;
  176.   x4 -= x6;
  177.   x6 = x5 + x7;
  178.   x5 -= x7;
  179.   
  180.   /* third stage */
  181.   x7 = x8 + x3;
  182.   x8 -= x3;
  183.   x3 = x0 + x2;
  184.   x0 -= x2;
  185.   x2 = (181*(x4+x5)+128)>>8;
  186.   x4 = (181*(x4-x5)+128)>>8;
  187.   
  188.   /* fourth stage */
  189.   blk[8*0] = iclp[(x7+x1)>>14];
  190.   blk[8*1] = iclp[(x3+x2)>>14];
  191.   blk[8*2] = iclp[(x0+x4)>>14];
  192.   blk[8*3] = iclp[(x8+x6)>>14];
  193.   blk[8*4] = iclp[(x8-x6)>>14];
  194.   blk[8*5] = iclp[(x0-x4)>>14];
  195.   blk[8*6] = iclp[(x3-x2)>>14];
  196.   blk[8*7] = iclp[(x7-x1)>>14];
  197. }
  198.  
  199. /* two dimensional inverse discrete cosine transform */
  200. #ifdef STORM
  201. void Fast_IDCT(short *block)
  202. #else
  203. void Fast_IDCT(block)
  204. short *block;
  205. #endif
  206. {
  207.   int i;
  208.  
  209.   for (i=0; i<8; i++)
  210.     idctrow(block+8*i);
  211.  
  212.   for (i=0; i<8; i++)
  213.     idctcol(block+i);
  214. }
  215.  
  216. void Initialize_Fast_IDCT()
  217. {
  218.   int i;
  219.  
  220.   iclp = iclip+512;
  221.   for (i= -512; i<512; i++)
  222.     iclp[i] = (i<-256) ? -256 : ((i>255) ? 255 : i);
  223. }
  224.